package com.wwwarehouse.taskcenter.fragment.creater_code;

import android.os.Bundle;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.eventbus_event.CardDeskEvent;
import com.wwwarehouse.common.fragment.CommonBasePagerFragment;
import com.wwwarehouse.common.tools.LogUtils;
import com.wwwarehouse.taskcenter.R;
import com.wwwarehouse.taskcenter.bean.PreviewWareCodeBean;
import com.wwwarehouse.taskcenter.bean.QueryWareBean;
import com.wwwarehouse.taskcenter.constant.TaskCenterConstant;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.EventBus;

/**
 * @创建者 chenchao.
 * @创建时间 2017/10/20 10:56
 * @描述 ${TODO}
 */

public class PreviewPositionCodeFragment extends CommonBasePagerFragment {

    private long mDataCodeRuleUkid;
    private Serializable mFrom;

    @Override
    public void setTitle() {
        if (peekFragment() instanceof PreviewPositionCodeFragment) {
            mActivity.setTitle(R.string.create_position_codes);
        }
    }

    @Override
    public void requestDatas() {
        super.requestDatas();

        if (getArguments() != null) {
            mDataCodeRuleUkid = getArguments().getLong("dataCodeRuleUkid");
            mFrom = getArguments().getSerializable("from");
        }
        requestHttp();
    }

    private void requestHttp() {
        Map<String, Object> wareCodeMap = new HashMap<>();
        wareCodeMap.put("dataCodeRuleUkid", mDataCodeRuleUkid);
        QueryWareBean queryWareBean = new QueryWareBean();
        queryWareBean.setPage(1);
        queryWareBean.setSize(15);
        wareCodeMap.put("query", queryWareBean);
        loadData(TaskCenterConstant.DATA_CODE, wareCodeMap, 0);
    }
    @Override
    public void onRefreshListener() {
        requestHttp();
    }

    @Override
    public void onLoadSuccess(String s, int i) {
        if (0 == i) {
            PreviewWareCodeBean previewWareCodeBean = JSON.parseObject(s, PreviewWareCodeBean.class);
            int total = previewWareCodeBean.getTotal();
            ArrayList<PreviewWareCodeBean.ListBean> list = (ArrayList<PreviewWareCodeBean.ListBean>) previewWareCodeBean.getList();
            Bundle wareCodeToChildBundle = new Bundle();
            wareCodeToChildBundle.putLong("dataCodeRuleUkid", mDataCodeRuleUkid);
            try {
                setData(total, 15, "com.wwwarehouse.taskcenter.fragment.creater_code.PreviewPositionCodeFragmentChild", wareCodeToChildBundle, list);
                showConfirmButton();
                setConfirmBtnText(mActivity.getResources().getString(R.string.confirm));
            } catch (Exception e) {
                LogUtils.showLog(e.toString());
            }
        }
        mConfirmBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ("function".equals(mFrom)) {
                    EventBus.getDefault().post(new CardDeskEvent(""));
                } else if ("task".equals(mFrom)) {
                    EventBus.getDefault().post(new CardDeskEvent("clear_back_stack"));
                }
            }
        });
    }



    @Override
    public void onLoadError(String s) {

    }

    @Override
    public void onDownPull() {
//        super.onDownPull();
    }
}
